import { cloneDeep } from 'lodash-es';
import { useAsync } from '@/hooks/use-asyns.js';

// 普通表格
const useTable = () => {
	return reactive({
		loading: false,
		data: [],
		totalRow: 0,
	});
};

/**
 * 请求
 * @param {*} request 请求方法
 * @param {*} requestData 请求数据
 * @param {*} data 接受者
 * @param {*} callback 回调方法
 * @param {*} loading
 */
const fetch = async function (request, requestData, data, callback, loading) {
	try {
		const res = await useAsync(request(unref(requestData)), loading);
		if (res.code === 200 && data) {
			isRef(data) ? (data.value = res.data) : (data = res.data);
		}
		callback && callback(res);
	} catch (error) {
		console.error(error);
	}
};

function pageCount(total, limit) {
	return total > 0 ? (total < limit ? 1 : total % limit ? parseInt(total / limit) + 1 : total / limit) : 0;
}

/**
 * 表格查询
 * @param {*} request  请求方法
 * @param {*} requestData  请求数据
 * @param {*} treeTable  表格
 * @param {Function} callback 回调方法
 */
const fetchPage = async (request, requestData, table, callback) => {
	try {
		table.loading = true;
		const { code, msg, data } = await request(unref(requestData));
		if (code === 200) {
			const { records, totalRow, pageNumber } = res.data;
			if (records.length === 0 && pageNumber > 1) {
				requestData.pageNumber = pageNumber - 1;
				return fetchPage(request, requestData, table, callback);
			} else {
				table.data = records;
				table.totalRow = totalRow;
				if (unref(requestData).totalRow) {
					isRef(requestData) ? (requestData.value.totalRow = totalRow) : (requestData.totalRow = totalRow);
				}
			}
			if (callback) callback(data);
		} else {
			$baseAlert.warning(msg);
		}
	} catch (error) {
		console.error(error);
	} finally {
		table.loading = false;
	}
};

/**
 * 列表查询
 * @param {*} request  请求方法
 * @param {*} requestData  请求数据
 * @param {*} treeTable  表格
 */
const fetchList = async (request, requestData, table, callback) => {
	try {
		table.loading = true;
		const { code, msg, data } = await request(unref(requestData));
		if (code === 200) {
			table.data = data;
			if (callback) callback(data);
		} else {
			$baseAlert.warning(msg);
		}
	} catch (error) {
		console.error(error);
	} finally {
		table.loading = false;
	}
};

// 树形表格
const useTreeTable = () => {
	return reactive({
		loading: false,
		data: [],
		fixedData: [],
	});
};

const useCloneDeep = (data) => {
	return cloneDeep(unref(data));
};

/**
 * 树形表格查询
 * @param {*} request  请求方法
 * @param {*} requestData  请求数据
 * @param {*} treeTable  树形表格
 * @param {Function} callback 回调函数
 */
const fetchTree = async (request, requestData, treeTable, callback) => {
	treeTable.loading = true;
	const { code, msg, data } = await useAsync(request(requestData));
	if (code === 200) {
		treeTable.data = data;
		treeTable.fixedData = data;
		if (callback) callback(data);
	} else {
		$baseAlert.warning(msg);
	}
	treeTable.loading = false;
};

// 添加/编辑
const handleEdit = (editRef, data) => {
	nextTick(() => unref(editRef).open(data));
};

/**
 * 移动
 * @param {*} request  请求方法
 * @param {*} requestData  请求数据
 * @param {*} treeTable  表格
 * @param {Function} callback 回调函数
 */
const handleMove = async (request, requestData, treeTable, callback) => {
	treeTable.loading = true;
	const { code, msg, data } = await request(requestData);
	if (code === 200) {
		$baseMsg.success(msg);
		if (callback) callback(data);
	} else {
		$baseAlert.warning(msg);
	}
	treeTable.loading = false;
};

/**
 * 删除操作
 * @param {*} request  删除的请求方法
 * @param {*} requestData  删除请求数据
 * @param {*} table  表格
 * @param {*} message 提示文字
 * @param {*} callback 回调函数
 */
const handleDelete = (request, requestData, table, message = '你确定要删除吗？', callback) => {
	if (requestData) {
		$baseConfirm.warning(message, '', () => {
			table.loading = true;
			request(requestData).then((res) => {
				const { code, msg } = res;
				if (code === 200) {
					$baseMsg.success(msg);
					if (callback) callback({ code, msg });
				} else {
					$baseAlert.warning(msg);
				}
				table.loading = false;
			});
		});
	} else {
		$baseMsg.error('未选中任何行');
	}
};

// 简单表格，树形表格，深度拷贝，分页请求，列表请求，树形请求
export { fetch, useTable, useTreeTable, useCloneDeep, fetchPage, fetchList, fetchTree, handleEdit, handleMove, handleDelete };
